{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "ename": "SyntaxError",
     "evalue": "invalid syntax (425449787.py, line 1)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;36m  Cell \u001b[1;32mIn[6], line 1\u001b[1;36m\u001b[0m\n\u001b[1;33m    elif what.get()==2:\u001b[0m\n\u001b[1;37m    ^\u001b[0m\n\u001b[1;31mSyntaxError\u001b[0m\u001b[1;31m:\u001b[0m invalid syntax\n"
     ]
    }
   ],
   "source": [
    " elif what.get()==2:\n",
    "        #绘制直线\n",
    "        lastDraw = canvas.create_line(X.get(),\n",
    "                                         Y.get(),\n",
    "                                         event.x,\n",
    "                                         event.y,\n",
    "                                         fill=foreColor)\n",
    "elif what.get()==3:\n",
    "        #绘制矩形\n",
    "        lastDraw = canvas.create_rectangle(X.get(),\n",
    "                                              Y.get(),\n",
    "                                              event.x,\n",
    "                                              event.y,\n",
    "                                              fill=backColor,\n",
    "                                              outline=foreColor)\n",
    "elif what.get()==6:\n",
    "        #绘制园\n",
    "        lastDraw = canvas.create_oval(\n",
    "                X.get(),\n",
    "                Y.get(),\n",
    "                event.x,\n",
    "                event.y,\n",
    "                fill=backColor,\n",
    "                outline=foreColor\n",
    "            )\n",
    "\n",
    "elif what.get()==5:\n",
    "        #橡皮，使用背景色填充10*10的矩形区域\n",
    "        canvas.create_rectangle(event.x-5,\n",
    "                                   event.y-5,\n",
    "                                   event.x+5,\n",
    "                                   event.y+5,\n",
    "                                   outline=backColor,\n",
    "                                   fill=backColor)\n",
    "canvas.bind('<B1-Motion>', onLeftButtonMove)\n",
    "\n",
    "#鼠标左键抬起，不允许再画图了\n",
    "def onLeftButtonUp(event):\n",
    "    canDraw.set(0)\n",
    "    global lastDraw\n",
    "    #防止切换图形时误删上次绘制的图形\n",
    "    lastDraw = 0\n",
    "canvas.bind('<ButtonRelease-1>', onLeftButtonUp)\n",
    "\n",
    "#创建菜单\n",
    "menu = tkinter.Menu(root, tearoff=0)\n",
    "#打开图像文件\n",
    "def Open():\n",
    "    filename = tkinter.filedialog.askopenfilename(title='Open Image',\n",
    "                            filetypes=[('image', '*.jpg *.png *.gif')])\n",
    "    if filename:\n",
    "        global image\n",
    "        image = tkinter.PhotoImage(file=filename)\n",
    "        canvas.create_image(80, 80, image=image)\n",
    "menu.add_command(label='Open', command=Open)\n",
    "\n",
    "def Save():\n",
    "    #获取客户区域位置和尺寸，并截图保存\n",
    "    left = int(root.winfo_rootx())\n",
    "    top = int(root.winfo_rooty())\n",
    "    width = root.winfo_width()\n",
    "    height = root.winfo_height()\n",
    "    im = ImageGrab.grab((left,top,left+width,top+height))\n",
    "    \n",
    "    #保存绘制的图片\n",
    "    filename = tkinter.filedialog.asksaveasfilename(title='保存图片',\n",
    "                                       filetypes=[('图片文件','*.png')])\n",
    "    if not filename:\n",
    "        return\n",
    "    if not filename.endswith('.png'):\n",
    "        filename = filename+'.png'\n",
    "    \n",
    "    im.save(filename)\n",
    "menu.add_command(label='Save', command=Save)\n",
    "\n",
    "#添加菜单，清除，遍历画布上的所有图像，逐个删除\n",
    "def Clear():\n",
    "    for item in canvas.find_all():\n",
    "        canvas.delete(item)\n",
    "menu.add_command(label='Clear', command=Clear)\n",
    "\n",
    "#添加分割线\n",
    "menu.add_separator()\n",
    "\n",
    "#创建子菜单，用来选择绘图类型\n",
    "menuType = tkinter.Menu(menu, tearoff=0)\n",
    "def drawCurve():\n",
    "    what.set(1)\n",
    "menuType.add_command(label='Curve', command=drawCurve)\n",
    "\n",
    "def drawLine():\n",
    "    what.set(2)\n",
    "menuType.add_command(label='Line', command=drawLine)\n",
    "\n",
    "\n",
    "def drawRectangle():\n",
    "    what.set(3)\n",
    "menuType.add_command(label='Rectangle', command=drawRectangle)\n",
    "\n",
    "def drawCircle():\n",
    "    what.set(6)\n",
    "menuType.add_command(label='园', command=drawCircle)\n",
    "\n",
    "def drawText():\n",
    "    global text\n",
    "    text = tkinter.simpledialog.askstring(title='Input what you want to draw', prompt='')\n",
    "    what.set(4)\n",
    "menuType.add_command(label='Text', command=drawText)\n",
    "\n",
    "menuType.add_separator()\n",
    "\n",
    "#选择前景色\n",
    "def chooseForeColor():\n",
    "    global foreColor\n",
    "    foreColor = tkinter.colorchooser.askcolor()[1]\n",
    "menuType.add_command(label='Choose Foreground Color',\n",
    "                       command=chooseForeColor)\n",
    "\n",
    "#选择背景色\n",
    "def chooseBackColor():\n",
    "    global backColor\n",
    "    backColor = tkinter.colorchooser.askcolor()[1]\n",
    "menuType.add_command(label='Choose Background Color',\n",
    "                       command=chooseBackColor)\n",
    "\n",
    "#橡皮\n",
    "def onErase():\n",
    "    what.set(5)\n",
    "menuType.add_command(label='Erase', command=onErase)\n",
    "\n",
    "menu.add_cascade(label='Type', menu=menuType)\n",
    "\n",
    "#鼠标右键抬起，弹出菜单\n",
    "def onRightButtonUp(event):\n",
    "    menu.post(event.x_root, event.y_root)\n",
    "canvas.bind('<ButtonRelease-3>', onRightButtonUp)\n",
    "\n",
    "canvas.pack(fill=tkinter.BOTH, expand=tkinter.YES)\n",
    "\n",
    "#启动应用程序\n",
    "root.mainloop()"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.9"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
